#!/bin/bash

# 清屏
clear

# 显示信息

echo -e "\033[41;33;33m 欢迎使用锦鲤 Discuz!服务器部署工具 \033[0m"

echo -e "\033[34m                                     版本v1.2 \033[0m"
echo ""

# 检查是否安装过

if [ -f "/var/discuz_deploy/docker-compose.yaml" ]; then
  while true
  do
    read -r -p "您本地已经安装过了，重装会清理掉所有数据，是否重新安装? [Y/n] " input

    case $input in
        [yY][eE][sS]|[yY])
        read -s -n1 -p "请您再次确认要删除所有数据并且重新安装，如果是请按任意键...否正请按Ctrl+C退出"
        cd /var/discuz_deploy/
        docker-compose down
        rm -rf /var/discuz_deploy/
        sed -i '/discuz_deploy/d' /etc/crontab
        break
        ;;

        [nN][oO]|[nN])
        echo "No"
        exit 1	       	
        ;;

        *)
        echo "输入错误"
        ;;
    esac
  done
fi

echo 1

# 选择协议
echo "请选择使用协议，本地测试建议选择http，线上运营请使用https"
echo "选择https需要服务器有公网IP。"

PROTOCOL="http https"
select PROTOCOL_SELECT in $PROTOCOL;do
if [ "$PROTOCOL_SELECT" = "http" ];then
  echo "http选择成功"
  break
elif [ "$PROTOCOL_SELECT" = "https" ];then
  echo "https选择成功"
  break
else
  echo "选择错误，请重新输入，选择1或者2"
fi
done

echo ""
echo "------------------"
echo ""
echo "下面需要您提供您的域名，使用空格分隔,最多支持两个域名，如果需要更多域名请手动添加"
echo -e "\033[41;33;33m 注意：第一个输入的为您的主域名，辅助域名会自动跳转到主域名  \033[0m"
echo "例如：bbs.mydome.com mydome.com"
echo "输入错误也不要担心，按住 “Ctrl+删除键” 即可删除,删除键就是平时删除文字的那个，学名Backspace"
# 输入域名
echo -n "请输入域名： "                   # 参数-n的作用是不换行，echo默认换行
read  NAME                                   # 把键盘输入放入变量NAME
echo "您的域名为 $NAME"     # 显示输入信息

# https邮箱
if [ "$PROTOCOL_SELECT" = "http" ];then
  echo ""
elif [ "$PROTOCOL_SELECT" = "https" ];then
  echo -e "\033[41;33;33m 您使用的为 $PROTOCOL_SELECT 协议，需要输入您的邮箱地址 \033[0m"
  echo -n "请输入邮箱地址： "                   # 参数-n的作用是不换行，echo默认换行
  read  EMAIL                                   # 把键盘输入放入变量NAME
  echo "您的邮箱为 $EMAIL"     # 显示输入信息
fi

## 拆分主域名和跳转域名
DOM_MIAN=""
DOM_JUMP=""

DOM_MIAN=`echo $NAME | awk '{print $1}'`
DOM_JUMP=`echo $NAME | awk '{print $2}'`
echo $DOM_MIAN
echo $DOM_JUMP


echo ""
echo "---------信息检查---------"
echo ""

# 邮箱是否输入判断
if [ "$PROTOCOL_SELECT" = "https" ];then
  if [ "$EMAIL" = "" ];then
    echo "邮箱未输入退出安装程序！"
    exit 0
  else
    echo "邮箱检查通过"
  fi
fi

# 域名是否输入判断
if [ "$NAME" = "" ];then
  echo "域名未输入退出安装程序！"
  exit 0
else
  echo "域名检查通过"
fi

# 邮箱格式检查
if [ "$PROTOCOL_SELECT" = "https" ];then
  n=`echo $EMAIL | egrep "^[a-zA-Z0-9_-\.\+]+@[a-zA-Z_-\.]+(\.[a-zA-Z0-9_-]+)+$"|wc -l`
  if [ $n == 0 ];then
    echo "邮箱不是正常邮箱地址，已退出！"
    exit 0
  else
    echo "邮箱验证通过"
  fi
fi

# 第二域名检查
if ["$DOM_JUMP" = ""];then
  echo "您第二域名未输入"
else
  echo "跳转域名为：$DOM_JUMP"
fi

echo ""
echo "--------信息检查完成----------"
echo ""

echo "恭喜你信息收集完毕！"
echo "您马上就可以安装，但是最后和您确认一下信息。"
if [ "$PROTOCOL_SELECT" = "http" ];then
  echo -e "\033[41;33;33m 您使用的是http协议，仅仅建议在测试环境（没有公网IP）使用，请您知晓。 \033[0m"
elif [ "$PROTOCOL_SELECT" = "https" ];then
  echo -e "\033[41;33;33m 您使用的为 $PROTOCOL_SELECT 协议，我们会帮您做好http自动跳转https \033[0m"
  echo "在服务器启动后，我们就会帮您申请好https证书，并且设置自动续期"
fi
echo "您的域名为 （主）$NAME "
echo "您的整个安装过程大概持续4分钟左右，请耐心等待"
echo "如果您确认上述信息正确，请按任意键继续安装，如果有错误，请按Ctrl+C，然后重新执行命令"
read -s -n1 -p "按任意键执行安装 ... "

## 安装
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2 git epel-* cronie lrzsz zip unzip
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安装 Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce docker-compose
# Step 4: 开启Docker服务
sudo systemctl start docker

# step 5: 设置开机启动
sudo systemctl enable docker

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://mirror.ccs.tencentyun.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl start crond
sudo systemctl enable crond

mkdir /var/discuz_deploy

git clone https://github.com/jinlicode/discuz_deploy.git /var/discuz_deploy

mkdir /var/discuz_deploy/code
mkdir /tmp/discuz_deploy_code

git clone https://gitee.com/3dming/DiscuzL.git /tmp/discuz_deploy_code

mv /tmp/discuz_deploy_code/upload/* /var/discuz_deploy/code/

rm -rf /tmp/discuz_deploy_code

chown -R 10000:10000 /var/discuz_deploy/code/

cd /var/discuz_deploy

mkdir -p /var/discuz_deploy/config/php
mkdir -p /var/discuz_deploy/config/mysql
mkdir -p /var/discuz_deploy/config/caddy
mkdir -p /var/discuz_deploy/db_bak

cp /var/discuz_deploy/example/php/* /var/discuz_deploy/config/php/
cp /var/discuz_deploy/example/mysql/* /var/discuz_deploy/config/mysql/

chmod +x /var/discuz_deploy/tools/db_bak.sh
echo "0 3 * * * root /var/discuz_deploy/tools/db_bak.sh" >> /etc/crontab

# 修复官方不更新版本问题
sed -i "s/20180101/20191201/" /var/discuz_deploy/code/source/discuz_version.php

# 替换文件

if [ "$PROTOCOL_SELECT" = "http" ];then
  cp /var/discuz_deploy/example/caddy/Caddyfile_http /var/discuz_deploy/config/caddy/Caddyfile
  sed -i "s/sub.yourdomain_mian.com/$DOM_MIAN/" /var/discuz_deploy/config/caddy/Caddyfile
  if ["$DOM_JUMP" = ""];then
    echo "跳过跳转域名设置"
  else
    sed -i "s/yourdomain.com/$DOM_JUMP/" /var/discuz_deploy/config/caddy/Caddyfile
  fi
  
elif [ "$PROTOCOL_SELECT" = "https" ];then
  cp /var/discuz_deploy/example/caddy/Caddyfile_https /var/discuz_deploy/config/caddy/Caddyfile
  sed -i "s/sub.yourdomain_mian.com/$DOM_MIAN/" /var/discuz_deploy/config/caddy/Caddyfile
  if ["$DOM_JUMP" = ""];then
    echo "跳过跳转域名设置"
  else
    sed -i "s/yourdomain.com/$DOM_JUMP/" /var/discuz_deploy/config/caddy/Caddyfile
  fi
  sed -i "s/admin@mail.com/$EMAIL/" /var/discuz_deploy/config/caddy/Caddyfile
fi

docker-compose up -d
echo -e "\033[41;33;33m -------------------------- \033[0m"
echo -e "\033[41;33;33m         安装完成 \033[0m"
echo -e "\033[41;33;33m -------------------------- \033[0m"

if [ "$PROTOCOL_SELECT" = "http" ];then
  echo -e "您的域名为：" "\033[41;33;33m http://$DOM_MIAN \033[0m" "不建议您在生产环境中使用"
elif [ "$PROTOCOL_SELECT" = "https" ];then
  echo -e "您的域名为：" "\033[41;33;33m https://$DOM_MIAN \033[0m" "首次启动会因为申请ssl证书较慢，请耐心等待1-2分钟"
fi
echo -e "\033[41;33;33m -------------------------- \033[0m"

echo -e "数据库地址：" "\033[41;33;33m mysql \033[0m"
echo -e "数据库名称：" "\033[41;33;33m discuz \033[0m"
echo -e "数据库账号：" "\033[41;33;33m root \033[0m"
echo -e "数据库密码：" "\033[41;33;33m discuz \033[0m"

echo -e "\033[41;33;33m -------------------------- \033[0m"
echo -e "不建议修改mysql数据库账号和密码，整个环境是一个单独私有网络，就算知道了也连不进来"
echo -e "如果有修改需求，后续升级需要根据git仓库变更进行升级。并且自己修改mysql备份工具"
echo "启动完成，绑定域名后直接访问安装即可"

